package com.cxxx.guns.modular.system.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cxxx.guns.core.base.tips.ErrorTip;
import com.cxxx.guns.core.exception.GunsException;
import com.cxxx.guns.core.exception.GunsExceptionEnum;
import com.cxxx.guns.modular.system.model.LoginLog;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 登录记录 Mapper 接口
 * </p>
 *
 * @author stylefeng
 * @since 2017-07-11
 */
public interface LoginLogMapper extends BaseMapper<LoginLog> {

    /**
     * 获取登录日志
     */
    List<Map<String, Object>> getLoginLogs(@Param("page") Page<LoginLog> page, @Param("beginTime") String beginTime,
                                           @Param("endTime") String endTime, @Param("logName") String logName, @Param("orderByField") String orderByField, @Param("isAsc") boolean isAsc);

    /**
     * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
     *
     * @author fengshuonan
     * @date 2016年11月12日 下午3:19:56
     */
    class BaseControllerExceptionHandler {

        private Logger log = LoggerFactory.getLogger(this.getClass());

        /**
         * 拦截业务异常
         */
        @ExceptionHandler(GunsException.class)
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        @ResponseBody
        public ErrorTip notFount(GunsException e) {
            log.error("业务异常:", e);
            return new ErrorTip(e.getCode(), e.getMessage());
        }

        /**
         * 拦截未知的运行时异常
         */
        @ExceptionHandler(RuntimeException.class)
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        @ResponseBody
        public ErrorTip notFount(RuntimeException e) {
            log.error("运行时异常:", e);
            return new ErrorTip(GunsExceptionEnum.SERVER_ERROR.getCode(), GunsExceptionEnum.SERVER_ERROR.getMessage());
        }

    }
}